<template>
  <j-modal
    :title="title"
    :width="1200"
    :visible="visible"
    :maskClosable="false"
    :confirmLoading="confirmLoading"
    switchFullscreen
    :ok-button-props="{ props: { disabled: disableSubmit } }"
    @ok="handleOk"
    @cancel="handleCancel">
    <template slot="footer">
      <a-button @click="handleCancel">关闭</a-button>
      <a-button v-if="!disableSubmit" type="primary" @click="handleOk">确定</a-button>
    </template>
    <a-spin :spinning="confirmLoading">
      <!-- 主表单区域 -->
      <a-form :form="form">
        <a-row>


          <a-col :xs="24" :sm="12">
            <a-form-item label="目标类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['typeId', validatorRules.typeId]" :trigger-change="true" dictCode="DataDictionary,description,dictionaryCode, dictionaryTypeId = OBJECT_TYPE" placeholder="请选择目标类型"/>
            </a-form-item>
</a-col>
          <a-col :xs="24" :sm="12">
            <a-form-item label="负责人" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-search-select-tag v-decorator="['partyId', validatorRules.partyId]" dict="Person,name,partyId,typeId = EMPLOYEE" />
            </a-form-item>
            </a-col>

          <a-col :xs="24" :sm="12">
            <a-form-item label="目标名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['objectiveName', validatorRules.objectiveName]" placeholder="请输入目标名称"></a-input>
            </a-form-item>
            </a-col>
            
         <a-col :xs="24" :sm="12">
           <a-form-item label="预计完成时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
             <j-date placeholder="请选择预计完成时间" v-decorator="['completionTime', validatorRules.completionTime]" :trigger-change="true" style="width: 100%"/>
           </a-form-item>
           </a-col>
           
          <a-col :span="24">
            <a-form-item label="备注" :labelCol="labelCol2" :wrapperCol="wrapperCol2">
              <a-textarea v-decorator="['description']" rows="4" placeholder="请输入备注"/>
            </a-form-item>
            </a-col>
          

        </a-row>
      </a-form>

      <!-- 子表单区域 -->
      <a-tabs v-model="activeKey" @change="handleChangeTabs">
        <a-tab-pane tab="关键指标" :key="refKeys[0]" :forceRender="true">
          <j-editable-table
            :ref="refKeys[0]"
            :tableName="refKeys[0]"
            :loading="hrKeyResultTable.loading"
            :columns="hrKeyResultTable.columns"
            :dataSource="hrKeyResultTable.dataSource"
            :maxHeight="300"
            :rowNumber="true"
            :rowSelection="true"
            :actionButton="true"/>
        </a-tab-pane>
        
      </a-tabs>

    </a-spin>
  </j-modal>
</template>

<script>

  import pick from 'lodash.pick'
  import { FormTypes,getRefPromise } from '@/utils/JEditableTableUtil'
  import { JEditableTableMixin } from '@/mixins/JEditableTableMixin'
  import { validateDuplicateValue } from '@/utils/util'
  import JDate from '@/components/jeecg/JDate'  
  import JDictSelectTag from "@/components/dict/JDictSelectTag"
  import JSearchSelectTag from '@/components/dict/JSearchSelectTag'
  export default {
    name: 'HrObjectiveModal',
    mixins: [JEditableTableMixin],
    components: {
      JDate,
      JDictSelectTag,
      JSearchSelectTag,
    },
    data() {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 6 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        labelCol2: {
          xs: { span: 24 },
          sm: { span: 3 },
        },
        wrapperCol2: {
          xs: { span: 24 },
          sm: { span: 20 },
        },
        // 新增时子表默认添加几行空数据
        addDefaultRowNum: 1,
        disableSubmit: false,
        validatorRules: {
          typeId: {
            rules: [
              { required: true, message: '请输入目标类型!'},
            ]
          },
          partyId: {
            rules: [
              { required: true, message: '请输入负责人!'},
            ]
          },
          objectiveName: {
            rules: [
              { required: true, message: '请输入目标名称!'},
            ]
          },
          completionTime: {
            rules: [
              { required: true, message: '请输入预计完成时间!'},
            ]
          },
        },
        refKeys: ['hrKeyResult', ],
        tableKeys:['hrKeyResult', ],
        activeKey: 'hrKeyResult',
        // 关键指标
        hrKeyResultTable: {
          loading: false,
          dataSource: [],
          columns: [
            {
              title: '目标',
              key: 'name',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
            {
              title: '备注',
              key: 'description',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
          ]
        },
        url: {
          add: "/online/special/api/createHrObjective",
          edit: "/online/special/api/updateHrObjective",
          hrKeyResult: {
            list: "/online/api/listHrKeyResultByHrObjectiveId",
            deleteBatch: "/online/api/deleteHrKeyResultByHrObjectiveBatch",
          },
        }
      }
    },
    methods: {
      getAllTable() {
        let values = this.tableKeys.map(key => getRefPromise(this, key))
        return Promise.all(values)
      },
      /** 调用完edit()方法之后会自动调用此方法 */
      editAfter() {
        let fieldval = pick(this.model,'typeId','partyId','objectiveName','description','completionTime','createDate','createBy')
        this.$nextTick(() => {
          this.form.setFieldsValue(fieldval)
        })
        // 加载子表数据
        if (this.model.id) {
          let params = { id: this.model.id }
          this.requestSubTableData(this.url.hrKeyResult.list, params, this.hrKeyResultTable)
        }
      },
      /** 整理成formData */
      classifyIntoFormData(allValues) {
        let main = Object.assign(this.model, allValues.formValue)
        return {
          ...main, // 展开
          HrKeyResult: allValues.tablesValue[0].values,
        }
      },
      validateError(msg){
        if(msg){
          this.isHandleOk = false;
        }
        this.$message.error(msg)
      },
     popupCallback(row){
       this.form.setFieldsValue(pick(row,'typeId','partyId','objectiveName','description','completionTime','createDate','createBy'))
     },

    }
  }
</script>

<style scoped>
</style>
